package com.stylefeng.guns.modular.system.task;

import java.net.URL;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.support.DateTimeKit;
import com.stylefeng.guns.core.util.HttpClientUtil;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.model.BusinessRankingDetailData;
import com.stylefeng.guns.modular.system.model.HyBusinessRankingData;
import com.stylefeng.guns.modular.system.model.Hybrank;
import com.stylefeng.guns.modular.system.model.HybrankDetail;
import com.stylefeng.guns.modular.system.service.IHybrankDetailService;
import com.stylefeng.guns.modular.system.service.IHybrankService;

@Service
public class HyBrankImportTask {

	private Logger log = LoggerFactory.getLogger(HyBrankImportTask.class);
	
	private static final HttpClientUtil clientUtil = new HttpClientUtil();
	
	@Autowired
	private IHybrankService hybrankService;
	
	@Autowired
	private IHybrankDetailService hybrankDetailService;

	public void execute(boolean isHoliday) {
		if(isHoliday){
			String day = DateTimeKit.format(new Date(), "yyyy-MM-dd");
			String url = ImportContants.getFormatHyBrankPathStr(1, day,day);
			HttpGet httpGet = HttpClientUtil.getGetRequest();
			try {
				httpGet.setURI(new URL(url).toURI());
				String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
				JSONObject jsonObj = JSON.parseObject(StringUtils.substringAfter(json, "="));
				if (jsonObj.getBooleanValue("success")) {
					int pages = jsonObj.getIntValue("pages");
					parseHyRankData(jsonObj.getString("data"),day);
					for(int i= 2; i <= pages ; i++){
						url = ImportContants.getFormatHyBrankPathStr(i, day,day);
						httpGet.setURI(new URL(url).toURI());
						json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
						jsonObj = JSON.parseObject(StringUtils.substringAfter(json, "="));
						if (jsonObj.getBooleanValue("success")) {
							parseHyRankData(jsonObj.getString("data"),day);
						}
					}
				}
			} catch (Exception e) {
				log.error("get hy brank data is error!" + e);
			}finally{
				httpGet.abort();
			}
		}
	}
	
	private void parseHyRankData(String json,String day){
		if(StringUtils.isNotEmpty(json)){
			List<HyBusinessRankingData> channelItemList = JSON.parseArray(json,HyBusinessRankingData.class);
			for(HyBusinessRankingData hybusinessRankingData : channelItemList){
				Hybrank hybrank = new Hybrank();
				hybrank.setgYybCode(hybusinessRankingData.getYybCode());
				hybrank.setgTdate(hybusinessRankingData.getTDate());
				hybrank = hybrankService.selectByDate(hybrank);
				if(hybrank == null){
					hybrank = new Hybrank();
					hybrank.setgBmoney(hybusinessRankingData.getBmoney());
					hybrank.setgJmMoney(hybusinessRankingData.getJmMoney());
					hybrank.setgSmoney(hybusinessRankingData.getSmoney());
					hybrank.setgSname(hybusinessRankingData.getSName());
					hybrank.setgTdate(hybusinessRankingData.getTDate());
					hybrank.setgYybBcount(hybusinessRankingData.getYybBCount());
					hybrank.setgYybScount(hybusinessRankingData.getYybSCount());
					hybrank.setgYybCode(hybusinessRankingData.getYybCode());
					hybrank.setgYybName(hybusinessRankingData.getYybName());
					hybrankService.insert(hybrank);
				}
				String url = ImportContants.getFormatHyBrankDetailPathStr(1, hybusinessRankingData.getYybCode());
				HttpGet httpGet = HttpClientUtil.getGetRequest();
				try {
					httpGet.setURI(new URL(url).toURI());
					String detail = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
					JSONObject jsonObj = JSON.parseObject(StringUtils.substringAfter(detail, "="));
					if (jsonObj.getBooleanValue("success")) {
						int pages = jsonObj.getIntValue("pages");
						parseHyRankDetailData(jsonObj.getString("data"),day);
						for(int i= 2; i <= pages ; i++){
							url = ImportContants.getFormatHyBrankDetailPathStr(i, hybusinessRankingData.getYybCode());
							httpGet.setURI(new URL(url).toURI());
							json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
							jsonObj = JSON.parseObject(StringUtils.substringAfter(json, "="));
							if (jsonObj.getBooleanValue("success")) {
								parseHyRankDetailData(jsonObj.getString("data"),day);
							}
						}
					}
				} catch (Exception e) {
					log.error("get hy brank data is error!" + e);
				}finally{
					httpGet.abort();
				}
			}
		}
	}
	
	private void parseHyRankDetailData(String json,String day){
		if(StringUtils.isNotEmpty(json)){
			List<BusinessRankingDetailData> channelItemList = JSON.parseArray(json,BusinessRankingDetailData.class);
			for(BusinessRankingDetailData businessRankingDetailData : channelItemList){
				if(DateTimeKit.format(new Date(), "yyyy-MM-dd").equals(day)){
					if(StringUtils.startsWith(businessRankingDetailData.getSCode(), "9")){
						continue;
					}
					HybrankDetail hybrankDetail = new HybrankDetail();
					hybrankDetail.setgSalesCode(businessRankingDetailData.getSalesCode());
					hybrankDetail.setgScode(businessRankingDetailData.getSCode());
					hybrankDetail.setgTDate(businessRankingDetailData.getTDate());
					hybrankDetail = hybrankDetailService.selectBySalesCodeDate(hybrankDetail);
					if(hybrankDetail == null){
						hybrankDetail = new HybrankDetail();
						hybrankDetail.setgSalesCode(businessRankingDetailData.getSalesCode());
						hybrankDetail.setgScode(businessRankingDetailData.getSCode());
						hybrankDetail.setgTDate(businessRankingDetailData.getTDate());
						hybrankDetail.setgBmoney(businessRankingDetailData.getBMoney());
						hybrankDetail.setgPbuy(businessRankingDetailData.getPBuy());
						hybrankDetail.setgSalesName(businessRankingDetailData.getSalesName());
						hybrankDetail.setgSname(businessRankingDetailData.getSName());
						hybrankDetail.setgSmoney(businessRankingDetailData.getSMoney());
						hybrankDetailService.insert(hybrankDetail);
					}
				}
			}
		}
	}
}
